#include <stdio.h>
#include <common.c>

unsigned int filter_allele(unsigned int reads[], unsigned int quality[], unsigned int num, float min_reads, float min_quality) {
    unsigned int ok_allele = 0;
    for(; num > 0; num --) {
        if((reads[num - 1] >= min_reads) && (quality[num - 1] >= min_quality))
            ok_allele ++;
        else
            quality[num - 1] = 0;//indicate..
    }
    return ok_allele;
}

void output_allele_with_quality(unsigned int chr, unsigned int pos, const unsigned char seq[][ALLELE_LEN], const unsigned int reads[], const unsigned int quality[], unsigned int num) {
    fprintf(stdout, "%u\t%u\t", chr, pos);
    for(; num > 0; num --) {
        if(quality[num - 1] > 0)
            fprintf(stdout, "%s:%u:%u/", seq[num - 1], reads[num - 1], quality[num - 1]);
    }
    fprintf(stdout, "\n");
}

int main(int argc, char *argv[]) {
    const unsigned int reads_total_min = atoi(argv[1]);
    const float reads_freq_min = atof(argv[2]);
    const unsigned int qual_total_min = atoi(argv[3]);
    const float qual_freq_min = atof(argv[4]);
    
    while(!feof(stdin)) {
        unsigned int chr, pos;
        unsigned char allele_info[ALLELE_INFO_LEN];
        fscanf(stdin, "%d\t%d\t%s\n", &chr, &pos, allele_info);
        
        unsigned char allele_seq[SITE_ALLELE_NUM][ALLELE_LEN];
        unsigned int allele_reads[SITE_ALLELE_NUM];
        unsigned int allele_quality[SITE_ALLELE_NUM];
        unsigned int num_allele = parse_allele_info(allele_info, allele_seq, allele_reads, allele_quality);

        unsigned int reads_total = sum_array(allele_reads, num_allele);
        unsigned int qual_total = sum_array(allele_quality, num_allele);

        if((reads_total >= reads_total_min) && (qual_total >= qual_total_min)) {
            unsigned int num_allele1 = filter_allele(allele_reads, allele_quality, num_allele, reads_total * reads_freq_min, qual_total * qual_freq_min);
            if(num_allele1 > 0)
                output_allele_with_quality(chr, pos, allele_seq, allele_reads, allele_quality, num_allele);
        }
    }
}
